package cn.tedu.mall.front.controller;

import cn.tedu.mall.common.restful.JsonPage;
import cn.tedu.mall.common.restful.JsonResult;
import cn.tedu.mall.front.service.IFrontAttributeService;
import cn.tedu.mall.front.service.IFrontSpuService;
import cn.tedu.mall.pojo.product.vo.AttributeStandardVO;
import cn.tedu.mall.pojo.product.vo.SpuListItemVO;
import cn.tedu.mall.pojo.product.vo.SpuStandardVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
public class FrontSpuController {

    @Autowired
    private IFrontSpuService spuService;

    @Autowired
    private IFrontAttributeService attributeService;

    /**
     * 根据categoryId查询 spu列表
     * @param categoryId
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/front/spu/list/{categoryId}")
    public JsonResult<JsonPage<SpuListItemVO>> listCatsSpus(
            @PathVariable("categoryId") Long categoryId,
            Integer page,
            Integer pageSize){
        JsonPage<SpuListItemVO> jsonPage = spuService.listSpuByCategoryId(categoryId, page, pageSize);
        return JsonResult.ok(jsonPage);
    }


    @GetMapping("/front/spu/{id}")
    public JsonResult<SpuStandardVO> getSpuStandard(@PathVariable("id") Long id){
        SpuStandardVO spuStandardVO = spuService.getFrontSpuById(id);
        return JsonResult.ok(spuStandardVO);
    }

    @GetMapping("/front/spu/template/{spuId}")
    public JsonResult<List<AttributeStandardVO>> getAllAttributes(@PathVariable Long spuId){
        List<AttributeStandardVO> attributeStandardVOS = attributeService.getSpuAttributesBySpuId(spuId);
        return JsonResult.ok(attributeStandardVOS);
    }
}
